////
/// Heading
/// @group Heading
////

//  th-heading()
/// Return a heading list. The first heading list will be returned
/// if no breakpoint is specified.
///
/// @since 0.0.10
/// @return {list} A heading list.
///
/// @param {list | string} $heading - A heading map key or list.
/// @param {number} $breakpoint [false] - A heading list breakpoint.
///
/// @example scss Return a heading list.
///   th-heading(h1)
///   // (10px 20px (30px 40px)
/// @example scss Return a heading list with breakpoint.
///   th-heading(h1, 768px)
///   // 50px 60px (70px 80px) 768px

@function th-heading(
  $heading,
  $breakpoint: false
){
  $breakpoint: th-core-context-get(breakpoint, $breakpoint);
  $heading: th-heading-get-map($heading);
  @if _th-heading-has-next($heading) {
    $heading: _th-heading-get-next($heading, $breakpoint);
  }
  @return $heading;
}

//  th-heading-get-map()
/// Return a heading map.
///
/// @since 0.0.10
/// @return {list} A heading map.
///
/// @param {string} $heading - A heading map key.
///
/// @example scss - Return h1 heading map.
///   th-heading-get-map(h1)
///   // 10px 20px (30px 40px), 50px 60px (70px 80px) 768px

@function th-heading-get-map(
  $heading
){
  @if string == type-of($heading) and map-has-key($th-headings, $heading) {
    $heading: map-get($th-headings, $heading);
  }
  @return $heading;
}

//  _th-heading-has-next()
/// Check a for multiple heading lists.
///
/// @since 0.0.10
/// @access private
/// @return {boolean} If heading has next or not.
///
/// @param {list | string} $heading - A heading map key or list.
///
/// @example
///   4 + 2 = 8
///   4 / 2 = 2
///
/// @example scss - Check heading with next.
///   // h1: (10px 20px (30px 40px), 50px 60px (70px 80px) 768px)
///   _th-heading-has-next(h1)
///   // true
/// @example scss - Check heading without next.
///   // h2: (10px 20px (30px 40px))
///   _th-heading-has-next(h2)
///   // false

@function _th-heading-has-next(
  $heading
){
  $heading: th-heading-get-map($heading);
  @if list-separator($heading) == comma {
    @return true;
  } @else {
    @return false;
  }
}

//  _th-heading-get-next()
/// Return a heading list by breakpoint.
///
/// @since 0.0.10
/// @access private
/// @return {list} A heading list.
///
/// @param {list | string} $heading - A heading map key or list.
/// @param {number} $breakpoint [false] - A heading list breakpoint.
///
/// @example scss - Return h1 heading map.
///   // h1: (10px 20px (30px 40px), 50px 60px (70px 80px) 768px)
///   _th-heading-get-next(h1, 768px)
///   // 50px 60px (70px 80px) 768px

@function _th-heading-get-next(
  $heading,
  $breakpoint: false
){
  $heading: th-heading-get-map($heading);
  @if $breakpoint {
    @each $heading-list in $heading {
      @if th-property(
        $property-name: breakpoint,
        $heading: $heading-list,
        $convert: false) == $breakpoint {
        $heading: $heading-list;
      }
    }
  } @else {
    $heading: nth($heading, 1);
  }
  @return $heading;
}

//  th-h()
/// Alias to th-heading() function.
///
/// @since 0.0.15

@function th-h(
  $heading,
  $breakpoint: false
){
  @return th-heading(
    $heading: $heading,
    $breakpoint: $breakpoint
  )
}